In [1]:
#import thinkdsp as td
#import thinkplot as tp
#import thinkstats2 as ts
#import thinkbayes as tb
In [2]:
import numpy as np
import matplotlib.pyplot as pl
import scipy.signal as sg
In [3]:
θ= np.linspace(0,1,1001)*2*np.pi
θ
Out[3]:
array([0.        , 0.00628319, 0.01256637, ..., 6.27061894, 6.27690212,
       6.28318531])
In [4]:
x= np.cos(θ)
x
Out[4]:
array([1.        , 0.99998026, 0.99992104, ..., 0.99992104, 0.99998026,
       1.        ])
In [5]:
pl.plot(x)
pl.grid()
In [6]:
θ= np.linspace(0,10,1001)*2*np.pi
x= np.cos(θ)
pl.plot(θ,x)
pl.grid()
In [7]:
f=  1            # cycle/sec, Hz
ω= f   *2*np.pi # radian/sec
ϕ=  0  *2*np.pi
t=  np.linspace(0,10,1001)

θ= ω*t + ϕ
pl.figure()
pl.plot(t,θ)

x= np.cos(θ)
#pl.figure()
pl.plot(t,x)
pl.grid()
In [8]:
def cosPlot(f=1, A=1):
    #f=  1            # cycle/sec, Hz
    ω= f   *2*np.pi # radian/sec
    ϕ=  0  *2*np.pi
    t=  np.linspace(0,10,1001)

    θ= ω*t + ϕ
    pl.figure()
    pl.plot(t,θ)
    pl.xlabel('t (sec)')
    pl.ylabel('θ (rad)')
    pl.grid()

    x= np.cos(θ) *A
    pl.figure()
    pl.plot(t,x)
    
    pl.xlabel('t (sec)')
    pl.ylabel('x')
    pl.grid()
    
    return x

cosPlot()
Out[8]:
array([1.        , 0.99802673, 0.9921147 , ..., 0.9921147 , 0.99802673,
       1.        ])
In [9]:
x= cosPlot(1)
x= cosPlot(2, 2)
x= cosPlot(5, 5)
In [10]:
x= cosPlot(10, 10)
In [11]:
from IPython.display import Audio
audio = Audio(data= x, rate= 16000)
audio
Out[11]:
In [12]:
def cosGen(f=1, A=1, T=1, Fs=10000):
    #f=  1            # cycle/sec, Hz
    ω= f   *2*np.pi # radian/sec
    ϕ=  0  *2*np.pi
    t=  np.linspace(0,T,T*Fs+1)
    θ= ω*t + ϕ
    x= np.cos(θ) *A    
    return x

x= cosGen(f= 1000)

audio = Audio(data= x, rate= 10000)
audio
Out[12]:
In [13]:
x1= cosGen(f= 1000)
x2= cosGen(f= 1500)
x3= cosGen(f= 2000)
x= np.concatenate([x1,x2,x3])

audio = Audio(data= x, rate= 10000)
audio
Out[13]:
In [14]:
## Major Scale

r= 2**(1/12)
r ### 1.059
majorScale= np.array([0,2,4,5,7,9,11,12])
fM= 1000*(r**majorScale)
fM=np.round(fM)

s= [cosGen(f= f) for f in fM]
x= np.concatenate(s)
audio = Audio(data= x, rate= 10000)
audio
Out[14]:
In [15]:
## Twinkle Star
[do, re, mi, fa, so, la, ti, Do]= [s[i] for i in range(8)]
In [16]:
twinkleStar= [
    do, do, so, so, la, la, so, so, 
    fa, fa, mi, mi, re, re, do, do,
    so, so, fa,fa,mi,mi,re,re,
    so, so, fa, fa, mi,mi,re,re,
    do, do, so, so, la, la, so, so, 
    fa, fa, mi, mi, re, re, do, do]

x= np.concatenate(twinkleStar)
a= Audio(data=x, rate=10000)
a
Out[16]:
In [17]:
import librosa 
librosa.output.write_wav('twinkleStar.wav', x, 10000, norm=False)
In [18]:
小蜜蜂= [
    so,mi,mi,mi,
    fa,re,re,re,
    do,re,mi,fa,
    so,so,so,so,
    
    so,mi,mi,mi,
    fa,re,re,re,
    do,mi,so,so,
    do,do,do,do]

x= np.concatenate(小蜜蜂)
a= Audio(data=x, rate=10000)
a
Out[18]:
In [19]:
pl.plot(x)
Out[19]:
[<matplotlib.lines.Line2D at 0x130e44f5a58>]
In [20]:
import librosa
In [21]:
librosa.output.write_wav('小蜜蜂.wav', x, 10000, norm=False)
In [22]:
y,fs= librosa.load('小蜜蜂.wav')
In [23]:
a= Audio(data=y, rate=fs)
a
Out[23]:
In [24]:
q= pl.specgram(y, 
             NFFT= 256, 
             Fs=   fs)